package com.upcode.weighing.admin.controller.sys;

import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.log.OpsLog;
import com.upcode.weighing.service.SysResService;
import com.upcode.weighing.service.SysRoleResService;
import com.upcode.weighing.vo.add.SysRoleResAddVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;


@RestController
@RequestMapping(value = "/menu")
@Api(value = "/menu",description = "资源接口",produces = MediaType.ALL_VALUE,tags = {"资源接口"})
public class MenuController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    private SysResService sysResService;
    @Autowired
    private SysRoleResService sysRoleResService;

    @OpsLog(operation = "资源接口/获取所有资源/查询" ,type = 1)
    @ApiOperation(value = "获取所有资源",notes ="")
    @PostMapping("/getRes")
    public Object getRes(){
        return success(sysResService.getRes());
    }

    @OpsLog(operation = "资源接口/根据角色ID资源/查询" ,type = 1)
    @ApiOperation(value = "根据角色ID资源",notes ="")
    @PostMapping("/getResByRole")
    public Object getResByRole(@RequestBody SysRoleResAddVo vo){
        return sysRoleResService.getResByRole(vo);
    }

    @OpsLog(operation = "资源接口/给角色分配资源/修改" ,type = 4)
    @ApiOperation(value = "给角色分配资源", notes = "添加", httpMethod = "POST")
    @PostMapping(value = "/addRoleRes")
    public Object addRoleResv(@RequestBody SysRoleResAddVo vo){
        int i=sysRoleResService.addRoleRes(vo);
        if(i>0){
            return success("添加成功");
        }
        return error("添加失败，请联系超级管理员");
    }

    @OpsLog(operation = "资源接口/修改角色资源/修改" ,type = 4)
    @ApiOperation(value = "修改角色资源", notes = "添加", httpMethod = "POST")
    @PostMapping(value = "/editRoleRes")
    public Object editRoleRes(@RequestBody SysRoleResAddVo vo){
        //解除之前的绑定
        sysRoleResService.delResRoleByRid(vo.getRoleId());
        //再重修分配资源
        if(vo.getResIds().length!=0){
            int i=sysRoleResService.addRoleRes(vo);
            if(i>0){
                return success("修改成功");
            }
        }else{
            return success("修改成功");
        }
        return error("修改失败，请联系管理员");
    }
    @ApiOperation(value = "根据用户id获取用户权限下的资源（菜单）",notes ="")
    @GetMapping(value = "/menuTreeByUserId/{userId}")
    public Object menuTree(@PathVariable("userId") @NotNull(message = "pc端用户id不能为空！") Long userId){
        return  success(sysResService.menuTreeByUserId(userId));
    }
}

